package com.dz.risk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.SysConstant;
import com.dz.risk.config.DailyMathUtil;
import com.dz.risk.dao.*;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.*;
import com.dz.risk.pojo.model.FormulaModel;
import com.dz.risk.service.ConfigService;
import com.dz.risk.service.PressureTestIndexValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PressureTestIndexValueServiceImpl implements PressureTestIndexValueService {
    @Autowired
    private BondRiskInformationSheetDao bondRiskInformationSheetDao;
    @Autowired
    private ReportFormsDao reportFormsDao;
    @Autowired
    private ReportFormsDataValueDao reportFormsDataValueDao;
    @Autowired
    private ConfigService configService;
    @Autowired
    private ListOfDefaultedBondsDao listOfDefaultedBondsDao;
    //计算多少次的循环
    private final int NUM = 46;




    /**
     * 根据公式查询数据
     * @param queryParams       {"value": null, "formulaDesc": "1+1", "formulaType": "0", "parseFormula": ["902000100000201", "902000100000201"], "formulaDetails": "select * from t_st_report_forms_data_value"}
     * @param request
     * @return
     */
    @Override
    public BigDecimal numberation(String queryParams, HttpServletRequest request) {
        FormulaModel formulaModel = JSONObject.parseObject(queryParams, FormulaModel.class);
        String formulaDetails = formulaModel.getFormulaDetails();
        return reportFormsDataValueDao.selectByString(formulaDetails);
    }



    /**
     * 数据入库
     * @param version
     * @param dateFormat
     * @param reportFormsDataValueEntities
     * @param map
     * @throws ParseException
     */
    public void insertDataValue(Long version, SimpleDateFormat dateFormat, List<ReportFormsDataValueEntity> reportFormsDataValueEntities, Map<String, String> map) throws ParseException {
        for (ReportFormsDataValueEntity formsDataValueEntity : reportFormsDataValueEntities) {
            formsDataValueEntity.setVersion(dateFormat.parse(String.valueOf(version)));
            if (formsDataValueEntity.getTableCellSerialNumber() != null) {
                formsDataValueEntity.setDataValue(map.get(formsDataValueEntity.getTableCellSerialNumber()));
            }

            reportFormsDataValueDao.insert(formsDataValueEntity);
        }
    }

    public String getReportFormsId(ReportFormsEntity reportFormsEntity, String type) {
        /**
         * 获取报表id
         */
        //TODO 以后需要用表查出来
        reportFormsEntity.setReportFormsName(type);
        return reportFormsDao.selectReportFormsId(reportFormsEntity);
    }

    /**
     * 获取indexId 计算序号
     * @param num
     * @return
     */
    public String getTableCellString(int num) {
        String tableStr = "90200010000";
        String lastStr = "01";
        String numStr = num > 9 ? String.valueOf(num) :"0"+num;
        return tableStr+numStr+lastStr;
    }


}
